import {encSha512} from "@/services/encrypto";
import axios from "axios";
import {getUrlFor} from "@/services/UrlConfig";
import type React from "react";
import {formatResult} from "@/services/Utils";

async function addDriver(info: Domain.Driver) {
  /*验证号的各个参数的完备性*/
  if (info.password)
    info.password=encSha512(info.password)
  const msg = await axios.post(getUrlFor("/driver"), info)
  return msg.data
}

async function fetchAllDriver(params: API.PageParams,
                             sort: Record<string, 'ascend'|'descend'|null>|null,
                             filter: Record<string, React.ReactText[]|null>|null) {
  const reqParams = {
    start: params.current,
    size: params.pageSize
  }
  console.log(sort, filter);
  const res = await axios.get(getUrlFor("/driver", reqParams))
  return formatResult(res);
}

const rmDriver = async (account: string|undefined) => {
  await axios.delete(getUrlFor("/driver/"+account));
}

const updateDriver = async (info: Domain.Driver) => {
  await axios.put(getUrlFor("/driver/"+info.account), info);
}

export {addDriver, fetchAllDriver, rmDriver, updateDriver}
